﻿#include "BrowserTableHandlerNCTMultiple.h"
#include "OperatorsInterface/OperAbstractNCT.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTMultipleNoise.h"
#include <QPushButton>

namespace GUI
{
    void BrowserTableHandlerNCTMultiple::updateTable()
    {
        if (_tableWidget == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);
        //获取物理属性管理器
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsNCTManager* mgr = phys->getFITKAcousticsNCTManager();
        if (mgr == nullptr) return;
        _maxTreatment = 0;
        QList<Acoustics::FITKAcousticsAbsNCT*>  list = mgr->getAcousticsNCTByType(
            Acoustics::FITKAcousticsAbsNCT::NCTType::MulitipleNCT);
        for (int i = 0; i < list.size(); ++i)
        {
            //转换为多层包
            Acoustics::FITKAcousticsNCTMultipleNoise* nct = dynamic_cast<Acoustics::FITKAcousticsNCTMultipleNoise*>(list.at(i));
            if (nct == nullptr) continue;
            //记录最大层数
            int layers = nct->getDataCount();
            if (layers > _maxTreatment)
                _maxTreatment = layers;
        }

        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Trim Coverage%") << tr("Treatments");
        for (int i = 0; i < _maxTreatment; ++i)
        {
            QString Layer = QString("Treatment-%1").arg(i + 1);
            headerLabels << Layer;
        }
        headerLabels << tr("Color") << tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }

    void BrowserTableHandlerNCTMultiple::updateData()
    {
        if (_tableWidget == nullptr) return;
        //获取物理属性管理器
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsNCTManager* mgr = phys->getFITKAcousticsNCTManager();
        if (mgr == nullptr) return;

        QList<Acoustics::FITKAcousticsAbsNCT*>  list = mgr->getAcousticsNCTByType(
            Acoustics::FITKAcousticsAbsNCT::NCTType::MulitipleNCT);
        //多层包数量
        const int n = list.size();
        _tableWidget->setRowCount(n);

        for (int i = 0; i < n; ++i)
        {
            //转换为多重包
            Acoustics::FITKAcousticsNCTMultipleNoise* nct = dynamic_cast<Acoustics::FITKAcousticsNCTMultipleNoise*>(list.at(i));
            if (nct == nullptr) continue;
            //数据ID
            const int id = nct->getDataObjectID();
            //填充表格，用type表示ID
            _tableWidget->setItem(i, 0, new QTableWidgetItem(nct->getDataObjectName(), id));
            double cov=0;//覆盖率
            _tableWidget->setItem(i, 2, new QTableWidgetItem(QString::number(nct->getDataCount()), id));
            //填充每一层的属性
            for (int j = 0; j < nct->getDataCount(); ++j)
            {
                auto data = nct->getDataByIndex(j);
                QString treatment = getTreatment(data);
                cov += data->getCoverage();
                _tableWidget->setItem(i, 2 + j + 1, new QTableWidgetItem(treatment, id));
            }
            _tableWidget->setItem(i, 1, new QTableWidgetItem(QString::number(cov*100), id));
            float color[3] = { 0,0,0 };
            nct->getColor(color);
            QPushButton* button = new QPushButton(_tableWidget);
            this->SetPushButtonColor(button, color[0] * 255, color[1] * 255, color[2] * 255);
            _tableWidget->setCellWidget(i, 2 + _maxTreatment + 1, button);
            connect(button, &QPushButton::clicked, this, [=] {this->onEditItemColor(button, nct); });
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }

    EventOper::OperAbstractNCT * BrowserTableHandlerNCTMultiple::getNCTOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractNCT>("OperNCTMultiple");
    }

    bool BrowserTableHandlerNCTMultiple::enableAttriSet()
    {
        QList<QTableWidgetItem*> selectItems = this->getSelectItems();
        if (selectItems.size() < 2) return false;

        QTableWidgetItem* item = selectItems.at(0);
        if (item == nullptr) return false;
        //名称列
        const int colIndex = item->column();
        //
        if (colIndex <3|| colIndex > 3+_maxTreatment ) return false;

        return true;
    }

    QString BrowserTableHandlerNCTMultiple::getTreatment(Acoustics::FITKAcousticsNCTMultipleNoiseWeight *data)
    {
        if (!data) return QString();
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return QString();
        Acoustics::FITKAcousticsNCTManager* mgr = phys->getFITKAcousticsNCTManager();
        if (mgr == nullptr) return QString();
        auto ntc = mgr->getDataByID(data->getNCTID());
        if (ntc)
            return ntc->getDataObjectName();
        return QString();
    }

}